import {message} from 'antd';
import {formatMessage} from './intl';
import {serviceActivityScope} from './constants';

export const newOrUpdateOrderValidator = (data, materials, workItems, vehicles, conditions, dealerQuotas) => {
    const requiredFields = [];
    if(!data || !data.code)
        requiredFields.push(
            formatMessage({
                id: 'validator.code',
                defaultMessage: '编号'
            })
        );
    if(!data || !data.serviceName)
        requiredFields.push(
            formatMessage({
                id: 'validator.serviceName',
                defaultMessage: '名称'
            })
        );
    if(!data || !data.serviceType)
        requiredFields.push(
            formatMessage({
                id: 'validator.serviceType',
                defaultMessage: '类型'
            })
        );
    if(!data || !data.dealerId)
        requiredFields.push(
            formatMessage({
                id: 'validator.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!data || !data.brandId)
        requiredFields.push(
            formatMessage({
                id: 'validator.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!data || !data.content)
        requiredFields.push(
            formatMessage({
                id: 'validator.content',
                defaultMessage: '活动内容'
            })
        );
    if(!data || !data.chargeMode)
        requiredFields.push(
            formatMessage({
                id: 'validator.chargeMode',
                defaultMessage: '计费模式'
            })
        );
    if(!data || !data.isRestrictedQuantity)
        requiredFields.push(
            formatMessage({
                id: 'validator.isRestrictedQuantity',
                defaultMessage: '是否限制台次'
            })
        );
    if(!data || (data.isRestrictedQuantity && !data.serviceQuantity))
        requiredFields.push(
            formatMessage({
                id: 'validator.serviceQuantity',
                defaultMessage: '可参加总台次'
            })
        );
    if(!data || !data.needMgmtFee)
        requiredFields.push(
            formatMessage({
                id: 'validator.needMgmtFee',
                defaultMessage: '计算备件管理费'
            })
        );
    if(!data || !data.scope)
        requiredFields.push(
            formatMessage({
                id: 'validator.scope',
                defaultMessage: '适用范围'
            })
        );
    if(!data || !data.startTime)
        requiredFields.push(
            formatMessage({
                id: 'validator.startTime',
                defaultMessage: '开始时间'
            })
        );
    if(!data || !data.endTime)
        requiredFields.push(
            formatMessage({
                id: 'validator.endTime',
                defaultMessage: '结束时间'
            })
        );
    if(requiredFields.length > 0) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.requied',
            defaultMessage: '必填'
        })}`);
        return false;
    }
    if(!materials || (materials && materials.length !== 0)) {
        if(materials.some(d => d.newPartId === undefined || d.newPartId === 0)) {
            message.warning(formatMessage({
                id: 'validator.message.newPartId',
                defaultMessage: '新件不能为空！'
            }));
            return false;
        }
        if(materials.some(d => d.quantity === null || d.quantity === 0)) {
            message.warning(formatMessage({
                id: 'validator.message.quantity',
                defaultMessage: '数量不能为0！'
            }));
            return false;
        }
        if(!materials.some(d => d.isCaused === true)) {
            message.warning(formatMessage({
                id: 'validator.message.isCaused',
                defaultMessage: '请选择一个材料为主损件'
            }));
            return false;
        }
    }
    if(data.scope === serviceActivityScope.指定车辆清单 && vehicles.length <= 0) {
        message.warning(formatMessage({
            id: 'validator.vehiclesDetails',
            defaultMessage: '服务活动指定车辆清单不能为空'
        }));
        return false;
    }
    if(data.scope === serviceActivityScope.指定车辆条件 && conditions.length <= 0) {
        message.warning(formatMessage({
            id: 'validator.conditionsDetails',
            defaultMessage: '服务活动条件清单不能为空'
        }));
        return false;
    }

    if(data.scope === serviceActivityScope.指定车辆条件 && conditions.find(d => d.conditionName === undefined || d.conditionName === null)) {
        message.warning(formatMessage({
            id: 'validator.message.conditionName',
            defaultMessage: '服务活动条件清单中条件名称不能为空！'
        }));
        return false;
    }
    if(data.scope === serviceActivityScope.指定车辆条件 && conditions.find(d => d.operator === undefined || d.operator === null)) {
        message.warning(formatMessage({
            id: 'validator.message.operator',
            defaultMessage: '服务活动条件清单中运算符不能为空！'
        }));
        return false;
    }
    if(data.scope === serviceActivityScope.指定车辆条件 && conditions.find(d => d.conditionValues === undefined || d.conditionValues === null)) {
        message.warning(formatMessage({
            id: 'validator.message.conditionValues',
            defaultMessage: '服务活动条件清单中值不能为空！'
        }));
        return false;
    }
    

    // if(!dealerQuotas || (dealerQuotas && dealerQuotas.length !== 0)) {
    //     if(materials.find(d => d.dealerId === undefined || d.dealerId === 0))
    //         message.warning(formatMessage({
    //             id: 'validator.message.dealerId',
    //             defaultMessage: '服务站配额清单中服务站不能为空！'
    //         }));
    //     if(materials.find(d => d.quota === undefined || d.quota === 0))
    //         message.warning(formatMessage({
    //             id: 'validator.message.quota',
    //             defaultMessage: '服务站配额清单中配额台数不能为空！'
    //         }));
    //     return false;
    // }
    return true;
};
